﻿/*
 * Copyright 2010 Pieter De Rycke
 *
 * This file is part of Frozen Bubble XNA.
 *
 * Frozen Bubble XNA is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2, as published by the Free Software Foundation.
 *
 * Frozen Bubble XNA is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Frozen Bubble XNA. If not, see 
 * <http://www.gnu.org/licenses/>.
 *
 * Artwork:
 *    Alexis Younes <73lab at free.fr>
 *      (everything but the bubbles)
 *    Amaury Amblard-Ladurantie <amaury at linuxfr.org>
 *      (the bubbles)
 *
 * Soundtrack:
 *    Matthias Le Bidan <matthias.le_bidan at caramail.com>
 *      (the three musics and all the sound effects)
 *
 * Design & Programming:
 *    Guillaume Cottenceau <guillaume.cottenceau at free.fr>
 *      (design and manage the project, whole Perl sourcecode)
 *
 * XNA and Windows Phone 7 port:
 *    Pieter De Rycke
 *
 * Websites:
 *    http://www.frozen-bubble.org/
 *    http://pieterderycke.wordpress.com/
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;

namespace FrozenBubble
{
    public class PauseScreen : GameScreen
    {
        private Texture2D _background;
        private Texture2D[] _animation;
        private SoundEffect _snore;

        private KeyboardState _oldInputState;

        private Vector2 _animationPosition;

        private int _index;
        private int _skip;

        private Random _random;

        public PauseScreen(Vector2 animationPosition)
        {
            _animationPosition = animationPosition;
            _random = new Random();
        }

        public override void LoadContent()
        {
            _background = Game.Content.Load<Texture2D>("Graphics/back_paused");
            _animation = new Texture2D[5];
            for (int i = 0; i < _animation.Length; i++)
                _animation[i] = Game.Content.Load<Texture2D>("Graphics/pause_" + (i + 1));
            _snore = Game.Content.Load<SoundEffect>("Audio/snore");
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch.Draw(_background, ScreenManager.ViewportRectangle, Color.White);
            SpriteBatch.Draw(_animation[_index], _animationPosition, Color.White);
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            if (!otherScreenHasFocus)
            {
                KeyboardState inputState = Keyboard.GetState();
                if (inputState.IsKeyUp(Keys.P) && _oldInputState.IsKeyDown(Keys.P))
                    ExitScreen();

                _oldInputState = inputState;
            }


            if (_skip > 30)
            {
                if (Storage.IsSoundEnabled && _random.NextDouble() < 0.2)
                    _snore.Play();

                _skip = 0;
                _index += 1;

                if (_index == _animation.Length)
                    _index -= 2;

            }
            else
                _skip++;
        }
    }
}
